package managedbean.SaleQuotation;

import entity.CRM.LineItem;
import entity.CRM.SaleInquiry;
import java.io.IOException;
import javax.faces.event.ActionEvent;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.PhaseEvent;
import session.stateless.CRM.DiscountBeanLocal;
import session.stateless.CRM.SaleQuotationBeanLocal;

@ManagedBean(name="addSQFromSI")
@ViewScoped
public class AddQuotationFromInquiryManagerBean implements Serializable {
    public AddQuotationFromInquiryManagerBean () {}
    
    @EJB
    private SaleQuotationBeanLocal saleQuotationBeanLocal;
    @EJB
    private DiscountBeanLocal discountBeanLocal;
    
    private SaleInquiry selectedSaleInquiry;
    private Long saleQuotationId;
    private List<LineItem> lineItems;
    private String statusMessage;
    private Integer validityPeriod;
    private List<Double> volumeDiscountRate;
    private Double finalPrice;
    
    public void initView(PhaseEvent event){        
        if (selectedSaleInquiry == null)                        
            selectedSaleInquiry = (SaleInquiry)FacesContext.getCurrentInstance().getExternalContext().getSessionMap().get("selectedSaleInquiry");            
        try
        {
                List<LineItem> temp_lineItems = new ArrayList<LineItem>();
                List<Double> temp_volumeDiscountRates = new ArrayList<Double>();
                
                for (LineItem temp_lineItem:selectedSaleInquiry.getLineItems())
                {
                    temp_lineItem.setFinalPrice(saleQuotationBeanLocal.calculateFinalPrice(selectedSaleInquiry.getId(), temp_lineItem.getProductPrice(), temp_lineItem.getLineItemQuantity()));
                    temp_lineItems.add(temp_lineItem);
                
                    Double temp_volumeDiscountRate=discountBeanLocal.calculateDiscountRate(temp_lineItem.getProductPrice().getProductType(), temp_lineItem.getLineItemQuantity());
                    temp_volumeDiscountRates.add(temp_volumeDiscountRate);
                }
            this.setLineItems(temp_lineItems);           
            this.setVolumeDiscountRate(temp_volumeDiscountRates);
            this.setFinalPrice(selectedSaleInquiry.getTotalPrice());
        }
        catch(Exception ex)
        {
            ex.printStackTrace();
        }        
    }
    
    public List<LineItem> getLineItems() {
        return lineItems;
    }
    public void setLineItems(List<LineItem> lineItems) {
        this.lineItems = lineItems;
    }
    
    public String getStatusMessage() {
        return statusMessage;
    }
    public void setStatusMessage(String statusMessage) {
        this.statusMessage = statusMessage;
    }

    public Double getFinalPrice() {
        return finalPrice;
    }

    public void setFinalPrice(Double finalPrice) {
        this.finalPrice = finalPrice;
    }

    public SaleInquiry getSelectedSaleInquiry() {
        return selectedSaleInquiry;
    }
    public void setSelectedSaleInquiry(SaleInquiry selectedSaleInquiry) {
        this.selectedSaleInquiry = selectedSaleInquiry;
    }

    public Long getSaleQuotationId() {
        return saleQuotationId;
    }
    public void setSaleQuotationId(Long saleQuotationId) {
        this.saleQuotationId = saleQuotationId;
    }

    public List<Double> getVolumeDiscountRate() {
        return volumeDiscountRate;
    }

    public void setVolumeDiscountRate(List<Double> volumeDiscountRate) {
        this.volumeDiscountRate = volumeDiscountRate;
    }

    public Integer getValidityPeriod() {
        return validityPeriod;
    }

    public void setValidityPeriod(Integer validityPeriod) {
        this.validityPeriod = validityPeriod;
    }
    
    
    public void saveNewSaleQuotation(ActionEvent event)
    {        
        try
        {              
            saleQuotationId = saleQuotationBeanLocal.convertToSaleQuotation(selectedSaleInquiry.getId(), lineItems, new Integer(0), validityPeriod);
            statusMessage = "New Sale Order Saved Successfully.";
            FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Add New Sale Quotation Result: " + statusMessage + " (New Sale Quotation ID: " + saleQuotationId + ")", ""));
        }
        catch(Exception ex) 
        { 
            ex.printStackTrace(); 
        } 
        
    }
    public void refresh(ActionEvent event) throws IOException
    {
        }
    
}
